/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models.DAOs;

import DBConnection.ConnectionFactory;
import Models.Magazines;
import Models.Products;
import Security.Encrypter;
import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author 0905
 */
public class Magazine_DAO extends Product_DAO {
    public boolean addMagazine(Magazines magazine){
        try{
            
            int productID = this.addProduct(magazine);
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
            
            PreparedStatement pstmtMag = conn.prepareStatement("INSERT INTO magazine(issue, editor, frequency, productID) VALUES(?,?,?,?)");
            pstmtMag.setString(i++, magazine.getIssue());
            pstmtMag.setString(i++, magazine.getEditor());
            pstmtMag.setString(i++, magazine.getFrequency());
            pstmtMag.setInt(i++, productID);  
            pstmtMag.executeUpdate();
            return true;
        }
        catch(Exception e){
            return false;
        }
    }
    
    public boolean updateMagazine(Magazines magazine, String magazineCode){
        try{
            
            String encryptedMagazine = new Encrypter().decrypt(magazineCode);
            magazine.setProductID(Integer.parseInt(encryptedMagazine));
            
            boolean updatedProduct = this.updateProduct(magazine);
            
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
            
            PreparedStatement pstmtMag = conn.prepareStatement("UPDATE magazine SET issue = ?, editor = ?, frequency = ? WHERE productID = ?");            
            pstmtMag.setString(i++, magazine.getIssue());
            pstmtMag.setString(i++, magazine.getEditor());
            pstmtMag.setString(i++, magazine.getFrequency());
            pstmtMag.setInt(i++, magazine.getMagazineID());
            pstmtMag.executeUpdate();
            
            return true;
        }
        catch(Exception e){
            
            return false;
        }
    }
    public ArrayList<Magazines> getAllMagazine(){
        
        Magazines magazine;
        ArrayList<Magazines> magList = null;
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            CallableStatement pstmt = conn.prepareCall("{ call GET_ALL_MAGAZINE() }");
            ResultSet rs = pstmt.executeQuery();
            magList = new ArrayList<Magazines>();
            Encrypter encrypt = new Encrypter();
            while(rs.next())
            {
                magazine = new Magazines();
                magazine.setProductCrypt(encrypt.encrypt(Integer.toString(rs.getInt("productID"))));
                
                magazine.setProductID(rs.getInt("productID"));
                magazine.setMagazineID(rs.getInt("magazineID"));
                magazine.setTitle(rs.getString("title"));
                magazine.setIssue(rs.getString("issue"));
                magazine.setPrice(rs.getFloat("price"));
                magazine.setSynopsis(rs.getString("synopsis"));
                magazine.setEditor(rs.getString("editor"));
                magazine.setFrequency(rs.getString("frequency"));
                System.out.println(magazine.getTitle());
                magList.add(magazine);
            }
            
            return magList;
        }
        catch(SQLException e){
        
            return magList;
        }
            
    }
    public Magazines getMagazine(int magazineID){
        
        Magazines magazine = null;
        
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM magazine b INNER JOIN products p ON b.productID = p.productID WHERE b.productID = ?");
            pstmt.setInt(1, magazineID);
            
            ResultSet rs = pstmt.executeQuery();
            if(rs.next())
            {
                magazine = new Magazines();
                magazine.setProductCrypt(new Encrypter().encrypt(Integer.toString(rs.getInt("productID"))));
                magazine.setMagazineID(rs.getInt("magazineID"));
                magazine.setTitle(rs.getString("title"));
                magazine.setIssue(rs.getString("issue"));
                magazine.setPrice(rs.getFloat("price"));
                magazine.setSynopsis(rs.getString("synopsis"));
                magazine.setEditor(rs.getString("editor"));
                magazine.setFrequency(rs.getString("frequency"));             
            }
            return magazine;           
        }
        catch(SQLException e){
        
            e.printStackTrace();
            return magazine;
        }    
    }
}
